package com.lzy.arithmetic;

/**
 * Author: lzy
 * Date:   2020/06/11 18:42
 * Description:
 **/
public class 分割链表 {

    public static ListNode partition(ListNode head, int x) {
        if (head == null) {
            return null;
        }

        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val < x) {
                ListNode nextNode = cur.next;
                cur.next = nextNode.next;

                nextNode.next = head;
                head = nextNode;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(6);
        ListNode l2 = new ListNode(4);
        ListNode l3 = new ListNode(5);
        ListNode l4 = new ListNode(6);
        ListNode l5 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;

        ListNode result = partition(l1, 5);
        while (result != null) {
            System.out.println(result.val);
            result = result.next;
        }
    }
}
